﻿/**
    版本：AS3.0

    功能：UTF-8转GB2312，BIG5,GBK类

*/
package zf.data.tools
{
	import flash.utils.ByteArray;
	/**
	 * UTF-8编码转换 
	 * @author mike.zhang
	 * 
	 */
	public class UrlMultiEncode
	{
		//public static function EncodeUtf8 (str : String):String
		//{
		//var oriByteArr : ByteArray = new ByteArray();
		//oriByteArr.writeUTFBytes (str);
		//var tempByteArr : ByteArray = new ByteArray();
		//trace (str);
		//for (var i = 0; i<oriByteArr.length; i++)
		//{
		//if (oriByteArr[i] == 194)
		//{
		//tempByteArr.writeByte (oriByteArr[i+1]);
		//i++;
		//}
		//else if (oriByteArr[i] == 195)
		//{
		//tempByteArr.writeByte (oriByteArr[i+1] + 64);
		//i++;
		//}
		//else
		//{
		//tempByteArr.writeByte (oriByteArr[i]);
		//}
		//}
		//tempByteArr.position = 0;
		//return tempByteArr.readMultiByte(tempByteArr.bytesAvailable,"chinese");
		//}

		/**
		 * 转换字符为GB2312 
		 * @param str 字符串
		 * @return GB2312编码字符串
		 * 
		 */
		public static function urlencodeGB2312 (str:String):String
		{
			var result:String ="";
			var byte:ByteArray =new ByteArray();
			byte.writeMultiByte (str,"gb2312");
			for (var i:int; i<byte.length; i++)
			{
				result += escape(String.fromCharCode(byte[i]));
			}
			return result;
		}

		/**
		 * 转换字符为BIG5 
		 * @param str 字符串
		 * @return BIG5编码字符串
		 * 
		 */
		public static function urlencodeBIG5 (str:String):String
		{
			var result:String ="";
			var byte:ByteArray =new ByteArray();
			byte.writeMultiByte (str,"big5");
			for (var i:int; i<byte.length; i++)
			{
				result += escape(String.fromCharCode(byte[i]));
			}
			return result;
		}

		/**
		 * 转换字符为GBK 
		 * @param str 字符串
		 * @return GBK编码字符串
		 * 
		 */
		public static function urlencodeGBK (str:String):String
		{
			var result:String ="";
			var byte:ByteArray =new ByteArray();
			byte.writeMultiByte (str,"gbk");
			for (var i:int; i<byte.length; i++)
			{
				result += escape(String.fromCharCode(byte[i]));
			}
			return result;
		}
		
		/**
		 * UTF8转换为UTF16 
		 * @param char 待转换字符
		 * @return String
		 * 
		 */
		public static function utf8to16(char:String):String
		{
			var out:Array = new Array();
			var len:uint = char.length;
			var i:uint = 0;
			var char2:int;
			var char3:int;
			while (i<len)
			{
				var c:int = char.charCodeAt(i++);
				switch (c >> 4)
				{
					case 0 :
					case 1 :
					case 2 :
					case 3 :
					case 4 :
					case 5 :
					case 6 :
					case 7 :
						// 0xxxxxxx  
						out[out.length] = char.charAt(i-1);
						break;
					case 12 :
					case 13 :
						// 110x xxxx   10xx xxxx  
						char2 = char.charCodeAt(i++);
						out[out.length] = String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
						break;
					case 14 :
						// 1110 xxxx  10xx xxxx  10xx xxxx  
						char2 = char.charCodeAt(i++);
						char3 = char.charCodeAt(i++);
						out[out.length] = String.fromCharCode(((c & 0x0F) << 12) |  
							((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0));
						break;
				}
			}
			return out.join('');
		}
		
		/**
		 * UTF16转换为utf8 
		 * @param char 待转换字符
		 * @return String
		 * 
		 */
		public static function utf16to8(char:String):String
		{
			var out:Array = new Array();
			var len:uint = char.length;
			for (var i:uint=0; i<len; i++)
			{
				var c:int = char.charCodeAt(i);
				if (c >= 0x0001 && c <= 0x007F)
				{
					out[i] = char.charAt(i);
				}
				else if (c > 0x07FF)
				{
					out[i] = String.fromCharCode(0xE0 | ((c >> 12) & 0x0F),  
						0x80 | ((c >>  6) & 0x3F),  
						0x80 | ((c >>  0) & 0x3F));
				}
				else
				{
					out[i] = String.fromCharCode(0xC0 | ((c >>  6) & 0x1F),  
						0x80 | ((c >>  0) & 0x3F));
				}
			}
			return out.join('');
		}

	}
}